Conditions | 1 |
Paths | 1 |
Total Lines | 286 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var assert = require('chai').assert, |
||
4 | describe('SourceDescription', function(){ |
||
5 | |||
6 | var fullJSON = { |
||
7 | resourceType: 'http://some/type', |
||
8 | citations: [ |
||
9 | { |
||
10 | lang: 'en', |
||
11 | value: 'Long source citation' |
||
12 | } |
||
13 | ], |
||
14 | mediaType: 'book', |
||
15 | about: 'http://a/resource', |
||
16 | mediator: { |
||
17 | resource: 'http://mediator' |
||
18 | }, |
||
19 | sources: [ |
||
20 | { |
||
21 | description: 'http://source/reference' |
||
22 | } |
||
23 | ], |
||
24 | analysis: { |
||
25 | resource: 'http://analysis' |
||
26 | }, |
||
27 | componentOf: { |
||
28 | description: 'http://container' |
||
29 | }, |
||
30 | titles: [ |
||
31 | { |
||
32 | lang: 'en', |
||
33 | value: 'Title' |
||
34 | }, |
||
35 | { |
||
36 | lang: 'es', |
||
37 | value: 'Titulo' |
||
38 | } |
||
39 | ], |
||
40 | notes: [ |
||
41 | { |
||
42 | subject: 'Note', |
||
43 | text: 'Some note text' |
||
44 | } |
||
45 | ], |
||
46 | attribution: { |
||
47 | created: 1234578129 |
||
48 | }, |
||
49 | rights: [ |
||
50 | { |
||
51 | resource: 'https://some/right' |
||
52 | } |
||
53 | ], |
||
54 | coverage: { |
||
55 | temporal: { |
||
56 | formal: '+2015' |
||
57 | }, |
||
58 | spatial: { |
||
59 | original: 'A place' |
||
60 | } |
||
61 | }, |
||
62 | descriptions: [ |
||
63 | { |
||
64 | value: 'A description' |
||
65 | } |
||
66 | ], |
||
67 | identifiers: { |
||
68 | $: 'identifier' |
||
69 | }, |
||
70 | created: 1000000, |
||
71 | modified: 11111111, |
||
72 | repository: { |
||
73 | resource: 'http://repository' |
||
74 | } |
||
75 | }; |
||
76 | |||
77 | it('Create plain', function(){ |
||
78 | assert.instanceOf(new GedcomX.SourceDescription(), GedcomX.SourceDescription, 'An instance of SourceDescription is not returned when calling the constructor with new.'); |
||
79 | assert.instanceOf(GedcomX.SourceDescription(), GedcomX.SourceDescription, 'An instance of SourceDescription is not returned when calling the constructor without new.'); |
||
80 | }); |
||
81 | |||
82 | it('Create with JSON', function(){ |
||
83 | var description = GedcomX.SourceDescription(fullJSON); |
||
84 | assert.equal(description.getResourceType(), 'http://some/type'); |
||
85 | assert.equal(description.getCitations().length, 1); |
||
86 | assert.equal(description.getCitations()[0].getLang(), 'en'); |
||
87 | assert.equal(description.getCitations()[0].getValue(), 'Long source citation'); |
||
88 | assert.equal(description.getMediaType(), 'book'); |
||
89 | assert.equal(description.getAbout(), 'http://a/resource'); |
||
90 | assert.equal(description.getMediator().getResource(), 'http://mediator'); |
||
91 | assert.equal(description.getSources().length, 1); |
||
92 | assert.equal(description.getSources()[0].getDescription(), 'http://source/reference'); |
||
93 | assert.equal(description.getAnalysis().getResource(), 'http://analysis'); |
||
94 | assert.equal(description.getComponentOf().getDescription(), 'http://container'); |
||
95 | assert.equal(description.getTitles().length, 2); |
||
96 | assert.equal(description.getTitles()[0].getLang(), 'en'); |
||
97 | assert.equal(description.getTitles()[0].getValue(), 'Title'); |
||
98 | assert.equal(description.getTitles()[1].getLang(), 'es'); |
||
99 | assert.equal(description.getTitles()[1].getValue(), 'Titulo'); |
||
100 | assert.equal(description.getNotes().length, 1); |
||
101 | assert.equal(description.getNotes()[0].getSubject(), 'Note'); |
||
102 | assert.equal(description.getNotes()[0].getText(), 'Some note text'); |
||
103 | assert.equal(description.getAttribution().getCreated().getTime(), 1234578129); |
||
104 | assert.equal(description.getRights().length, 1); |
||
105 | assert.equal(description.getRights()[0].getResource(), 'https://some/right'); |
||
106 | assert.equal(description.getCoverage().getTemporal().getFormal(), '+2015'); |
||
107 | assert.equal(description.getCoverage().getSpatial().getOriginal(), 'A place'); |
||
108 | assert.equal(description.getDescriptions().length, 1); |
||
109 | assert.equal(description.getDescriptions()[0].getValue(), 'A description'); |
||
110 | assert.equal(description.getIdentifiers().identifiers.$, 'identifier'); |
||
111 | assert.equal(description.getCreated(), 1000000); |
||
112 | assert.equal(description.getModified(), 11111111); |
||
113 | assert.equal(description.getRepository().getResource(), 'http://repository'); |
||
114 | }); |
||
115 | |||
116 | it('Create with mixed', function(){ |
||
117 | var description = GedcomX.SourceDescription({ |
||
118 | resourceType: 'http://some/type', |
||
119 | citations: [ |
||
120 | GedcomX.SourceCitation({ |
||
121 | lang: 'en', |
||
122 | value: 'Long source citation' |
||
123 | }) |
||
124 | ], |
||
125 | mediaType: 'book', |
||
126 | about: 'http://a/resource', |
||
127 | mediator: { |
||
128 | resource: 'http://mediator' |
||
129 | }, |
||
130 | sources: [ |
||
131 | GedcomX.SourceReference({ |
||
132 | description: 'http://source/reference' |
||
133 | }) |
||
134 | ], |
||
135 | analysis: { |
||
136 | resource: 'http://analysis' |
||
137 | }, |
||
138 | componentOf: { |
||
139 | description: 'http://container' |
||
140 | }, |
||
141 | titles: [ |
||
142 | GedcomX.TextValue({ |
||
143 | lang: 'en', |
||
144 | value: 'Title' |
||
145 | }), |
||
146 | { |
||
147 | lang: 'es', |
||
148 | value: 'Titulo' |
||
149 | } |
||
150 | ], |
||
151 | notes: [ |
||
152 | GedcomX.Note({ |
||
153 | subject: 'Note', |
||
154 | text: 'Some note text' |
||
155 | }) |
||
156 | ], |
||
157 | attribution: { |
||
158 | created: 1234578129 |
||
159 | }, |
||
160 | rights: [ |
||
161 | GedcomX.ResourceReference({ |
||
162 | resource: 'https://some/right' |
||
163 | }) |
||
164 | ], |
||
165 | coverage: GedcomX.Coverage({ |
||
166 | temporal: { |
||
167 | formal: '+2015' |
||
168 | }, |
||
169 | spatial: { |
||
170 | original: 'A place' |
||
171 | } |
||
172 | }), |
||
173 | descriptions: [ |
||
174 | { |
||
175 | value: 'A description' |
||
176 | } |
||
177 | ], |
||
178 | identifiers: { |
||
179 | $: 'identifier' |
||
180 | }, |
||
181 | created: 1000000, |
||
182 | modified: 11111111, |
||
183 | repository: GedcomX.ResourceReference({ |
||
184 | resource: 'http://repository' |
||
185 | }) |
||
186 | }); |
||
187 | assert.equal(description.getResourceType(), 'http://some/type'); |
||
188 | assert.equal(description.getCitations().length, 1); |
||
189 | assert.equal(description.getCitations()[0].getLang(), 'en'); |
||
190 | assert.equal(description.getCitations()[0].getValue(), 'Long source citation'); |
||
191 | assert.equal(description.getMediaType(), 'book'); |
||
192 | assert.equal(description.getAbout(), 'http://a/resource'); |
||
193 | assert.equal(description.getMediator().getResource(), 'http://mediator'); |
||
194 | assert.equal(description.getSources().length, 1); |
||
195 | assert.equal(description.getSources()[0].getDescription(), 'http://source/reference'); |
||
196 | assert.equal(description.getAnalysis().getResource(), 'http://analysis'); |
||
197 | assert.equal(description.getComponentOf().getDescription(), 'http://container'); |
||
198 | assert.equal(description.getTitles().length, 2); |
||
199 | assert.equal(description.getTitles()[0].getLang(), 'en'); |
||
200 | assert.equal(description.getTitles()[0].getValue(), 'Title'); |
||
201 | assert.equal(description.getTitles()[1].getLang(), 'es'); |
||
202 | assert.equal(description.getTitles()[1].getValue(), 'Titulo'); |
||
203 | assert.equal(description.getNotes().length, 1); |
||
204 | assert.equal(description.getNotes()[0].getSubject(), 'Note'); |
||
205 | assert.equal(description.getNotes()[0].getText(), 'Some note text'); |
||
206 | assert.equal(description.getAttribution().getCreated().getTime(), 1234578129); |
||
207 | assert.equal(description.getRights().length, 1); |
||
208 | assert.equal(description.getRights()[0].getResource(), 'https://some/right'); |
||
209 | assert.equal(description.getCoverage().getTemporal().getFormal(), '+2015'); |
||
210 | assert.equal(description.getCoverage().getSpatial().getOriginal(), 'A place'); |
||
211 | assert.equal(description.getDescriptions().length, 1); |
||
212 | assert.equal(description.getDescriptions()[0].getValue(), 'A description'); |
||
213 | assert.equal(description.getIdentifiers().identifiers.$, 'identifier'); |
||
214 | assert.equal(description.getCreated(), 1000000); |
||
215 | assert.equal(description.getModified(), 11111111); |
||
216 | assert.equal(description.getRepository().getResource(), 'http://repository'); |
||
217 | }); |
||
218 | |||
219 | it('Build', function(){ |
||
220 | var description = GedcomX.SourceDescription() |
||
221 | .setResourceType('http://some/type') |
||
222 | .addCitation(GedcomX.SourceCitation().setLang('en').setValue('Long source citation')) |
||
223 | .setMediaType('book') |
||
224 | .setAbout('http://a/resource') |
||
225 | .setMediator(GedcomX.ResourceReference().setResource('http://mediator')) |
||
226 | .addSource(GedcomX.SourceReference().setDescription('http://source/reference')) |
||
227 | .setAnalysis(GedcomX.ResourceReference().setResource('http://analysis')) |
||
228 | .setComponentOf(GedcomX.SourceReference().setDescription('http://container')) |
||
229 | .addTitle(GedcomX.TextValue().setLang('en').setValue('Title')) |
||
230 | .addTitle(GedcomX.TextValue().setLang('es').setValue('Titulo')) |
||
231 | .addNote(GedcomX.Note().setSubject('Note').setText('Some note text')) |
||
232 | .setAttribution(GedcomX.Attribution().setCreated(1234578129)) |
||
233 | .addRight(GedcomX.ResourceReference().setResource('https://some/right')) |
||
234 | .setCoverage( |
||
235 | GedcomX.Coverage() |
||
236 | .setTemporal(GedcomX.Date().setFormal('+2015')) |
||
237 | .setSpatial(GedcomX.PlaceReference().setOriginal('A place')) |
||
238 | ) |
||
239 | .addDescription(GedcomX.TextValue().setValue('A description')) |
||
240 | .setIdentifiers(GedcomX.Identifiers({ |
||
241 | $: 'identifier' |
||
242 | })) |
||
243 | .setCreated(1000000) |
||
244 | .setModified(11111111) |
||
245 | .setRepository(GedcomX.ResourceReference().setResource('http://repository')); |
||
246 | assert.equal(description.getResourceType(), 'http://some/type'); |
||
247 | assert.equal(description.getCitations().length, 1); |
||
248 | assert.equal(description.getCitations()[0].getLang(), 'en'); |
||
249 | assert.equal(description.getCitations()[0].getValue(), 'Long source citation'); |
||
250 | assert.equal(description.getMediaType(), 'book'); |
||
251 | assert.equal(description.getAbout(), 'http://a/resource'); |
||
252 | assert.equal(description.getMediator().getResource(), 'http://mediator'); |
||
253 | assert.equal(description.getSources().length, 1); |
||
254 | assert.equal(description.getSources()[0].getDescription(), 'http://source/reference'); |
||
255 | assert.equal(description.getAnalysis().getResource(), 'http://analysis'); |
||
256 | assert.equal(description.getComponentOf().getDescription(), 'http://container'); |
||
257 | assert.equal(description.getTitles().length, 2); |
||
258 | assert.equal(description.getTitles()[0].getLang(), 'en'); |
||
259 | assert.equal(description.getTitles()[0].getValue(), 'Title'); |
||
260 | assert.equal(description.getTitles()[1].getLang(), 'es'); |
||
261 | assert.equal(description.getTitles()[1].getValue(), 'Titulo'); |
||
262 | assert.equal(description.getNotes().length, 1); |
||
263 | assert.equal(description.getNotes()[0].getSubject(), 'Note'); |
||
264 | assert.equal(description.getNotes()[0].getText(), 'Some note text'); |
||
265 | assert.equal(description.getAttribution().getCreated().getTime(), 1234578129); |
||
266 | assert.equal(description.getRights().length, 1); |
||
267 | assert.equal(description.getRights()[0].getResource(), 'https://some/right'); |
||
268 | assert.equal(description.getCoverage().getTemporal().getFormal(), '+2015'); |
||
269 | assert.equal(description.getCoverage().getSpatial().getOriginal(), 'A place'); |
||
270 | assert.equal(description.getDescriptions().length, 1); |
||
271 | assert.equal(description.getDescriptions()[0].getValue(), 'A description'); |
||
272 | assert.equal(description.getIdentifiers().identifiers.$, 'identifier'); |
||
273 | assert.equal(description.getCreated(), 1000000); |
||
274 | assert.equal(description.getModified(), 11111111); |
||
275 | assert.equal(description.getRepository().getResource(), 'http://repository'); |
||
276 | }); |
||
277 | |||
278 | it('toJSON', function(){ |
||
279 | var description = GedcomX.SourceDescription(fullJSON); |
||
280 | assert.deepEqual(description.toJSON(), fullJSON); |
||
281 | }); |
||
282 | |||
283 | it('constructor does not copy instances', function(){ |
||
284 | var obj1 = GedcomX.SourceDescription(); |
||
285 | var obj2 = GedcomX.SourceDescription(obj1); |
||
286 | assert.strictEqual(obj1, obj2); |
||
287 | }); |
||
288 | |||
289 | }); |